package com.jyu.config;

import com.jyu.domain.Position;
import com.jyu.service.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;

@Component
public class CleanPosBean {

    @Autowired
    private PositionService positionService;

    //秒 分 小时 月份中的日期 月份 星期中的日期 年份
    //每天凌晨0点执行一次
    @Scheduled(cron = "0 0 0 * * ?")
    public void print(){
        LocalDateTime now = LocalDateTime.now();
        List<Position> sy = positionService.list();
        for (Position p: sy) {
            if(p.getCreateTime() != null){
                LocalDateTime createTime = p.getCreateTime();
                Duration duration = Duration.between(createTime,now);
                long days = duration.toDays();
                if(days >= 20){
                    positionService.delete(p.getId());
                    System.out.println("已删除");
                }
            }
        }
    }

}